package com.zsc.newbuds.controller;

import com.zsc.newbuds.entity.Recycle;
import com.zsc.newbuds.entity.RecyclingArea;
import com.zsc.newbuds.entity.Shop;
import com.zsc.newbuds.entity.User;
import com.zsc.newbuds.service.impl.*;
import com.zsc.newbuds.utils.RedisUtil;
import org.apache.shiro.web.session.HttpServletSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class MainController {

    @Autowired
    RecycleServiceImpl recycleService;
    @Autowired
    RecyclingAreaServiceImpl recyclingAreaService;
    @Autowired
    UserServiceImpl userService;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    ShopServiceImpl shopService;

    @Autowired
    ExchangeRecordServiceImpl exchangeRecordService;

    @RequestMapping("/donationExplain")
    public String toDonationExplain(){ return "redirect:main/donationExplain.html";}

    @RequestMapping("/main/donationExplain.html")
    public String donationExplain(){ return "main/donationExplain";}

    @RequestMapping("/shop")
    public String toShop(){
        return "redirect:main/shop.html";
    }

    @RequestMapping("/main/shop.html")
    public String toMainShop(Model model){
        List<Shop> goodslist=shopService.getGoodsMainList(0,8);
        System.out.println(goodslist.size());
        model.addAttribute("goodslist",goodslist);
        return "main/shop";
    }



    @ResponseBody
    @RequestMapping("/getGoods")
    public Map<String,Object> getGoods(Integer page){
        List<Shop> AllPageList=shopService.getGoodsList();
        System.out.println("page="+page);
        System.out.println(page*8+8);
        List<Shop> list=shopService.getGoodsMainList(page*8,page*8);
        Map<String,Object> map = new HashMap<>();
        System.out.println("Allpagelist="+AllPageList.size());
        Double pages=Math.ceil(AllPageList.size()/8.0);
        map.put("pages",pages);
        map.put("data",list);
        return map;
    }

//    @RequestMapping("/exchangeRecord")
//    public String toUserExchangeRecord(){
//        return "redirect:user/exchangeRecord";
//    }
    @RequestMapping("/user/exchangeRecord")
    public String toExRecord(){
        return "user/exchangeRecord";
    }

    @RequestMapping("/edit")
    public String toEdit(){
        return "redirect:user/edit.html";
    }
    @RequestMapping("user/edit.html")
    public String toMainEdit(){
        return "user/edit";
    }

    @RequestMapping("/myInfo")
    public String toMyInfo(){ return "redirect:user/myInfo.html";}
    @RequestMapping("user/myInfo.html")
    public String toUserInfo(Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        model.addAttribute(user);
        return "user/myInfo";
    }

    @ResponseBody
    @GetMapping("/main/getIntegral")
    public int getIntegral(HttpSession session){
        User user = (User) session.getAttribute("user");
        User user1 = (User)redisUtil.get("user:"+user.getUserId().toString());

        return user1.getIntegral();
    }

    @ResponseBody
    @PostMapping("/goodsExchange")
    public int goodsExchange(HttpSession session,Integer goodsId){
        User userId = (User) session.getAttribute("user");
        User user = (User)redisUtil.get("user:"+userId.getUserId().toString());
        System.out.println(goodsId);
        //减少商品数量
        shopService.goodsExchange(goodsId);
        //减少用户积分数量
        userService.updateUserIntegral(user.getUserId(),goodsId);
        //生成记录
        exchangeRecordService.addExchangeRecord(goodsId,user.getUserId());
        return 1;
    }

    @ResponseBody
    @GetMapping("/main/getRecyclingAreaList")
    public List<RecyclingArea> getRecyclingAreaList(){
        return recyclingAreaService.getAreaList();
    }

    @ResponseBody
    @GetMapping("/main/getRecycleData")
    public List getRecycleData(){
        String key = "list:userList";
        List list = new LinkedList();
        List<Recycle> recycles = recycleService.getRecycleList();
        for(Recycle recycle:recycles){
            Map<String, Object> map = new LinkedHashMap<>();
            Integer userId = recycle.getUserId();
            User user = (User)redisUtil.hget(key,userId.toString());
            map.put("name",user.getRealName());
            map.put("weight",recycle.getRecycleWeight());
            map.put("clothes",recycle.getClothesNumber());
            map.put("books",recycle.getRecycleNumber());
            map.put("address",recycle.getRecycleArea());
            map.put("time",recycle.getRecycleTime());
            list.add(map);
        }
        return list;
    }


    @ResponseBody
    @GetMapping("/main/getIpIsExist")
    public int getIpIsExist(HttpServletRequest request){
        int flag = 0;
        String ip = getIpAddress(request);
        System.out.println(ip);
        String key = "ip:"+ip;
        if(redisUtil.hasKey(key)){
            System.out.println("存在");
            flag = 1;
        }else {
            redisUtil.set(key,"true");
            System.out.println("不存在");
        }
        System.out.println(flag);
        return flag;
    }

    /**
     * 获取真实ip
     * @param request
     * @return
     */
    private String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
         }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
         }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
         }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
         }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return "0:0:0:0:0:0:0:1".equals(ip)?"127.0.0.1":ip;
     }
}
